using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ChatSocketService
{
    public static class MessageSerializer
    {

        #region Utils

        #region SerializeMessage

        public static byte[] SerializeMessage(ChatMessage msg)
        {

            using (MemoryStream m = new MemoryStream())
            {

                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(m, msg);

                return m.ToArray();

            }

        }

        #endregion

        #region DeserializeMessage

        public static ChatMessage DeserializeMessage(byte[] buffer)
        {

            using (MemoryStream m = new MemoryStream())
            {

                m.Write(buffer, 0, buffer.Length);
                m.Position = 0;

                BinaryFormatter bin = new BinaryFormatter();

                return (ChatMessage)bin.Deserialize(m);

            }

        }

        #endregion

        #endregion
    }
}